#include "mousetracker.h"
#include "mousetrackingworker.h"
#include <QDebug>
#include <cmath>

MouseTracker* MouseTracker::instance = nullptr;

MouseTracker::MouseTracker(QObject *parent)
    : QObject(parent),workerThread(new QThread()), lastX(0), lastY(0), currentX(0), currentY(0)
{
    // 创建并启动 MouseTrackingWorker 线程
    worker = new MouseTrackingWorker();

    worker->moveToThread(workerThread);
    workerThread->start(QThread::TimeCriticalPriority);  // 启动工作线程

    QMetaObject::invokeMethod(worker , "init" ,  Q_ARG(QString , devicePath));
}

MouseTracker::~MouseTracker()
{
    qInfo() << "MouseTracker::~MouseTracker";
    workerThread->quit();
    workerThread->wait();  // 等待线程结束
    delete worker;
}

double MouseTracker::getMoveDistance()
{
    updatemouceMoveDistance(worker->getMoveDistance());
    return mouceMoveDistance();
}

int MouseTracker::getMoveDistanceAct()
{
    double distance =  getMoveDistance();
    qInfo() << "getMoveDistanceAct " <<distance << " [" << get_fabricSpacing() << "]";
    if(distance > get_fabricSpacing() * get_fabricSpacingOnce())
    {
        return 0;
    }else if(distance < get_fabricSpacing() * get_fabricSpacingStop())
    {
        return 1;
    }
    else{
        return -1;
    }
}
